package com.bosssoft.hr.jobmonitor.core.Timer;

import com.bosssoft.hr.jobmonitor.config.common.SysConfig;
import com.bosssoft.hr.jobmonitor.core.auth.AuthInterceptor;
import com.bosssoft.hr.jobmonitor.core.file.FileTool;
import com.bosssoft.hr.jobmonitor.core.mail.EmailTool;
import com.bosssoft.hr.jobmonitor.entity.Task;
import com.bosssoft.hr.jobmonitor.entity.User;
import com.bosssoft.hr.jobmonitor.util.LoggerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/***
 * @Author lys(cherish)
 * @Description 提醒用户线程
 * @Date  2019/2/26
 **/
public class RemindMailRunnable implements Runnable{

    private static final Logger logger= LoggerFactory.getLogger(AuthInterceptor.class);

    //任务
    private Task task;
    //文件检测工具
    private CheckFileTaskTool checkFileTool;
    //邮件处理工具
    private EmailTool emailTool;
    //文件处理工具
    private FileTool fileTool;

    public RemindMailRunnable(Task task,CheckFileTaskTool checkFileTool, EmailTool emailTool, FileTool fileTool) {
        this.task=task;
        this.checkFileTool=checkFileTool;
        this.emailTool=emailTool;
        this.fileTool=fileTool;
    }

    /***
     * @Author lys(cherish)
     * @Description 提醒邮件定时发送
     * @Date  2019/2/26
     * @Param []
     * @return void
     **/
    @Override
    public void run() {
        SysConfig sysconfig = fileTool.getSysConfig();
        HashMap<String, Object> valueMap = new HashMap<>();
        valueMap.put("htmlTitle",sysconfig.getMailContent().getHtmlTitle());
        valueMap.put("mailTitle",sysconfig.getMailContent().getMailTitle());
        valueMap.put("mailContent",sysconfig.getMailContent().getMialContent());
        List<User> noSubmitUsers = checkFileTool.checkNoSubmit(task);
        logger.info(LoggerUtil.fmtLog("开始定时发送邮件"));
        ArrayList<String> addrList = new ArrayList<>();
        if(noSubmitUsers.size()!=0){
            for (int i=0;i<noSubmitUsers.size();i++){
                addrList.add(noSubmitUsers.get(i).getEmailaddr());
            }
        }
        if(addrList.size()!=0){
            valueMap.put("to",(String[])addrList.toArray(new String[0]));
            emailTool.sendSimpleMail(valueMap);
        }
    }
}
